package com.mrkun.vueshop.mapper;

import com.mrkun.vueshop.DTO.FundInfoChartDTO;
import com.mrkun.vueshop.DTO.FundInfoDTO;
import com.mrkun.vueshop.model.FundInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author by yong
 * @date 2020/3/16
 */
@Mapper
public interface FundInfoMapper {

    @Select("select * from fund_info a left join fund b where b.name like #{query}")
    List<FundInfo> findAll(@Param("query")String query);

    @Select("select * from fund_info where id = #{id}")
    FundInfo findOne(@Param("id")Integer id);

    @Insert("insert into fund_info (fund_id,every_date,net_asset_value,increases) values (#{fundId},#{everyDate},#{netAssetValue},#{increases})")
    void addFundInfo(FundInfo fundInfo);

    @Select("select * from fund_info where every_date = #{date} and fund_id = #{id}")
    FundInfo findToday(@Param("date")String date,@Param("id")Integer id);

    @Select("<script>" +
            "select a.id,a.name,a.registered_capital,b.every_date,b.net_asset_value,b.increases from" +
            " fund a left join fund_info b on a.id = b.fund_id" +
            " where every_date = #{date}" +
            "<if  test=\"fundType != null\" >" +
            "and a.type = #{fundType}" +
            "</if>" +
            "<if  test=\"query != null and query != '%%'\" >" +
            "and a.name like #{query}" +
            "</if>" +
            " order by increases desc" +
            "</script>")
    List<FundInfoDTO> getAll(@Param("date")String date,@Param("fundType")String fundType,@Param("query")String query);

    @Select("select increases from fund_info where fund_id = #{fundId} and every_date = #{date}")
    BigDecimal getIncrease(@Param("fundId")Integer fundId,@Param("date")String date);

    @Select("select b.id fundId,b.name fundName,a.every_date date,a.net_asset_value nav,a.increases " +
            "from fund_info a left join fund b on a.fund_id = b.id where a.fund_id = #{fundId}")
    List<FundInfoChartDTO> findChartData(@Param("fundId") Integer fundId);
}
